﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BabyFood.Data;
using BabyFood.Entities;

public partial class HinhAnhND : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.Params["IdHA"] != null)
            {
                H_IdBoSuuTap.Value = Request.Params["IdHA"].ToString();
                View_BST(Convert.ToInt32(H_IdBoSuuTap.Value));
            }
            Pager1.CurrentIndex = 1;
            HienThiAnhDep(Pager1.CurrentIndex);
        }
        
        Pager2.CurrentIndex = 1;
        Bind_Data(Pager2.CurrentIndex);
    }

    protected void HienThiAnhDep(int PageNo)
    {
        int TotalRows = 0;
        string WhereClause = "1=1";
        TList<BoSuuTap> TList_Catalog = DataRepository.BoSuuTapProvider.GetPaged(WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager1.PageSize, out TotalRows);
        dlSoBuuTap.DataSource = TList_Catalog;
        dlSoBuuTap.DataBind();
        Pager1.ItemCount = TotalRows;
        H_PageCount.Value = Pager1.PageCount.ToString();
    }

    public string GetImageBoSuuTap(object IdBoSuuTap)
    {
        int TotalRows = 0;
        TList<HinhAnh> Tlist_HinhAnh = DataRepository.HinhAnhProvider.GetPaged("IdBoSuuTap='" + IdBoSuuTap + "'", "NgayCapNhat DESC", 0, 1, out TotalRows);
        return Tlist_HinhAnh.Count > 0 ? Common.sPathUpBoSuuTap + "/" + IdBoSuuTap + "/Full/" + Tlist_HinhAnh[0].AnhDaiDien : "~/Images/no_image.jpg";
    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            HienThiAnhDep(Pager1.CurrentIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("HinhAnhND.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }

    protected void cmdNext_Click(object sender, ImageClickEventArgs e)
    {
        if (Pager1.CurrentIndex < Convert.ToInt32(H_PageCount.Value))
        {
            Pager1.CurrentIndex = Pager1.CurrentIndex + 1;
            HienThiAnhDep(Pager1.CurrentIndex);
        }
    }

    protected void cmdPrevious_Click(object sender, ImageClickEventArgs e)
    {
        if (Pager1.CurrentIndex > 1)
        {
            Pager1.CurrentIndex = Pager1.CurrentIndex - 1;
            HienThiAnhDep(Pager1.CurrentIndex);
        }
    }

    protected void View_BST(int IdBST)
    {
        BoSuuTap entHinhAnh = DataRepository.BoSuuTapProvider.GetById(IdBST);
        if (entHinhAnh != null)
        {
            Session["Title"] = entHinhAnh.Ten;
        }
        imgAnhDep.ImageUrl = GetImageBoSuuTap(H_IdBoSuuTap.Value);
        lblbst.Text = Server.HtmlDecode(entHinhAnh.TomTat);
        lblNoiDungTin.Text = Server.HtmlDecode(entHinhAnh.DienGiai);
        int TotalRows = 0;
        //TList<CommentAnhDep> TList_TinTucs = DataRepository.CommentAnhDepProvider.GetPaged("IdAnhDep = " + Convert.ToInt32(H_IdBoSuuTap.Value), "", 0, 0, out TotalRows);
        //// Retrieve the Parent List
        //lblComment.Text = TotalRows.ToString();

        //int ChiaSe = 0;
        //TList<ThichAnhDep> TList_Thich = DataRepository.ThichAnhDepProvider.GetPaged("IdAnhDep = " + Convert.ToInt32(H_IdBoSuuTap.Value), "", 0, 0, out ChiaSe);
        //// Retrieve the Parent List

        //lblChiaSe.Text = ChiaSe.ToString();
    }

    protected void cmdSend_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            CaptchaControl1.ValidateCaptcha(txtCaptcha.Text);
            if (CaptchaControl1.UserValidated)
            {
                //CommentAnhDep entComment = new CommentAnhDep();
                //entComment.IdAnhDep = Convert.ToInt32(H_IdBoSuuTap.Value);
                //entComment.Ten = Common.StandardString(txtName.Text);
                //entComment.Mail = Common.StandardString(txtEmails.Text);
                //entComment.TrangThai = true;
                //entComment.NoiDung = Server.HtmlEncode(txtNoiDung.Text);
                //entComment.NgayCapNhat = Common.GetServerDateTime();
                //updateStatus = DataRepository.CommentAnhDepProvider.Insert(entComment);

                if (!updateStatus)
                    WebMessage.Show("Cập nhật Comment thất bại!");
                else
                {
                    WebMessage.Show("Cập nhật Comment thành công!");
                    Response.Redirect("~/HinhAnhND.aspx?IdImage=" + Convert.ToInt32(H_IdBoSuuTap.Value));
                    
                }
                if (updateStatus)
                {
                    txtEmails.Text = "";
                    txtName.Text = "";
                    txtNoiDung.Text = "";
                    txtCaptcha.Text = "";
                    WebMessage.Show("Cập nhật Comment thành công!");
                }
                else
                    WebMessage.Show("Cập nhật Comment thất bại!");
            }
            else
            {
                lblResult.Text = "Bạn đã gõ sai mã bảo vệ";
                lblResult.ForeColor = System.Drawing.Color.Red;
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog("HinhAnhND.aspx.cs", "cmdSend_Click", ex.ToString());
            WebMessage.Show("Cập nhật Comment thất bại!");
        }
    }

    /// <summary>
    /// Bind Comment
    /// </summary>
    /// <param name="PageNo"></param>
    protected void Bind_Data(int PageNo)
    {
        try
        {
            //facebook.NavigateUrl = "http://www.facebook.com/sharer.php?u=" + Common.sPathUpLienKet + "/TinTucND.aspx?Id="+ H_CatalogId.Value.ToString();
            //twiter.NavigateUrl = "http://twitter.com/home?status=" + Common.sPathUpLienKet + "/TinTucND.aspx?Id="+ H_CatalogId.Value.ToString();
            //zingme.NavigateUrl = "http://wb.me.zing.vn/index.php?wb=" + Common.sPathUpLienKet + "/TinTucND.aspx?Id=" + H_CatalogId.Value.ToString() + "&t=js&c=share_button";
            int TotalRows = 0;
            string WhereClause = "1=1 and IdAnhDep=" + Convert.ToInt32(H_IdBoSuuTap.Value);
            DataSet dsAnhDep = DataRepository.Provider.ExecuteDataSet("CommentAnhDep_GetPaged", WhereClause, "NgayCapNhat DESC", PageNo - 1, Pager2.PageSize);
            dlcomment.DataSource = dsAnhDep.Tables[0];
            dlcomment.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsAnhDep.Tables[1].Rows[0][0]);
            }
            catch (Exception)
            {
            }
            Pager2.ItemCount = TotalRows;
            if (TotalRows <= Pager2.PageSize)
                Pager2.Visible = false;
            else
                Pager2.Visible = true;
        }
        catch (Exception)
        {
        }
    }


    protected void Pager2_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
        Pager2.CurrentIndex = currnetPageIndex;
        Bind_Data(currnetPageIndex);
    }

    public string GetHinhAnhFull(object Ten)
    {
        return "Upload/BoSuuTap/" + H_IdBoSuuTap.Value + "/Full/" + Ten;
    }

    public string GetHinhAnhThumb(object Ten)
    {
        return Common.sPathUpBoSuuTap + "/" + H_IdBoSuuTap.Value + "/Thumb/" + Ten;
    }

    public string GetNoiDung(object noidung)
    {
        string sub = Server.HtmlDecode((Convert.ToString(noidung)));
        return sub;
    }

    public string GetLienKet()
    {
        return Common.sPathUpLienKet;
    }

    public string GetTieuDe(object Char)
    {
        string s = Char.ToString();
        return Common.ConvertToUnSign(s);
    }
}